package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.OilDepot;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.BOilDepot;
import cn.eonshine.cuba.lsms.modular.basic.entity.Office;
import cn.eonshine.cuba.lsms.modular.basic.service.IBOilDepotService;
import cn.eonshine.cuba.lsms.modular.basic.service.IOfficeService;
import cn.eonshine.cuba.lsms.modular.basic.service.IOilDepotService;

@Service
public class OilDepotService extends CommonService<OilDepot> implements IOilDepotService {
	
	@Autowired
	private IOfficeService officeService;
	
	@Autowired
	private IBOilDepotService oilDepotService;
	
	private final String listUrl = "/oilDepot/list";
	private final String updateUrl = "/oilDepot/update";
	
	public void requestOil() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.DEPOSITE_NUM);
	}
	
	@Override
	protected Object dataHandle(OilDepot oilDepot) throws Exception {
		QueryWrapper<Office> entity = new QueryWrapper<Office>();
		Office info = null;
		if(StringUtils.isBlank(oilDepot.getId())) {
			entity.eq("CODE", oilDepot.getCode());
			entity.eq("TYPE", InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_OILDEPOT);
			info = officeService.getOne(entity);
		}else {
			info = officeService.getById(oilDepot.getId());
		}

		boolean isInsert = false;

		if (info == null) {
			info = new Office();
			isInsert = true;
		}
		
		info.setType(InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_OILDEPOT);
		info.setCode(StringUtils.trim(oilDepot.getCode()));
		info.setName(StringUtils.trim(oilDepot.getName()));
		info.setFullName(StringUtils.trim(oilDepot.getFullName()));
		if(StringUtils.isNotBlank(oilDepot.getUseable())) {
			info.setUseable(oilDepot.getUseable());
		}else {
			info.setUseable(InfStatus.USEABLE_YES);
		}
		info.setIsFromInf(InfStatus.IS_FROM_INTERFACE);
		
		if(isInsert) {
			info.setRemarks(StringUtils.trim(oilDepot.getName()));
			info.setParentId(InfStatus.LSMS_OFFICE_PARENT.ROOT);
			info.setParentIds(InfStatus.LSMS_OFFICE_PARENT.ROOT+InfStatus.SEPARATOR_CHAR);
			info.setCreateBy(InfStatus.CREATE_BY);
			info.setRemarks(oilDepot.getRemarks());
			if(oilDepot.getCreateDate() == null) {
				info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
			}else {
				info.setCreateDate(oilDepot.getCreateDate());
			}
		}
		
		info.setUpdateBy(InfStatus.UPDATE_BY);
		info.setUpdateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
		
		if (isInsert) {
			officeService.save(info);
			BOilDepot bOilDepot = new BOilDepot();
			bOilDepot.setId(info.getId());
			bOilDepot.setCreateBy(InfStatus.CREATE_BY);
			bOilDepot.setCreateDate(info.getCreateDate());
			bOilDepot.setUpdateBy(InfStatus.UPDATE_BY);
			bOilDepot.setUpdateDate(info.getUpdateDate());
			oilDepotService.save(bOilDepot);
		} else {
			officeService.updateById(info);
			BOilDepot bOilDepot = oilDepotService.getById(info.getId());
			if(bOilDepot != null) {
				bOilDepot.setUpdateBy(InfStatus.UPDATE_BY);
				bOilDepot.setUpdateDate(info.getUpdateDate());
				oilDepotService.save(bOilDepot);
			}
		}
		return info;
	}
	
	@Override
	protected List<OilDepot> feadBackDataHandle(Object obj, OilDepot oilDepot, List<OilDepot> feadBackDataLists)
			throws Exception {
		Office info = (Office) obj;
		OilDepot tmpBack = new OilDepot();
		tmpBack.setCode(oilDepot.getCode());
		tmpBack.setName(oilDepot.getName());
		tmpBack.setDataInputDatetime(oilDepot.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		tmpBack.setId(info.getId()); // lsms id
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
